
// #include "xsdk_config.h"

#include "xapp_config.h"

#if USER_CHECK_ENABLED( DND )

#include "xapp_access.h"
#include "xapp_DND.h"
#include "xapp_clock.h"
#include "xapp_profile.h"

#if USER_CHECK_LOG_ENABLED( DND )
#include "log.h"
#define LOG     LOG_INFO 
#define LOG0    LOG_INFO0 
#define LOG1    LOG_INFO1
#define LOG2    LOG_INFO2
#define LOG3    LOG_INFO3
#else 
#define LOG(...) 
#define LOG0(...)
#define LOG1(...)
#define LOG2(...)
#define LOG3(...)
#endif 

static xapp_task_dnd_t                     m_task_dnd; 

void xapp_DND_load_from_profile(void)
{
    const profile_DND_t *pDND = xapp_profile_get_DND();

    m_task_dnd.enabled      = pDND->enabled > 0 ? true : false;
    m_task_dnd.start        = (pDND->start.hour << 8) | pDND->start.minute;
    m_task_dnd.end          = (pDND->stop.hour << 8 ) | pDND->stop.minute;

    m_task_dnd.actived      = false;

    if( m_task_dnd.enabled )
    {
        const datetime_t *pdt = xapp_clock_get_now();
        xapp_DND_on_time_update(  );
    }
}



bool xapp_DND_check_is_actived(void)
{
    return m_task_dnd.enabled && m_task_dnd.actived ;
}

void xapp_DND_on_time_update( void )
{
    if( !m_task_dnd.enabled )
    {
        return;
    }
    
    const datetime_t* dt = xapp_clock_get_now();
    uint16_t hm = dt->hour << 8 | dt->minute;
    
    if( m_task_dnd.start > m_task_dnd.end )
    {
        if( hm >= m_task_dnd.start || hm <= m_task_dnd.end )
        {
            if( !m_task_dnd.actived )
            {
                m_task_dnd.actived = true;
                xapp_access_DND_enter();
            }
        }
        else 
        {
            if( m_task_dnd.actived )
            {
                m_task_dnd.actived = false;
                xapp_access_DND_exit();
            }
        }
    }
    else 
    {
        if( hm >= m_task_dnd.start && hm <= m_task_dnd.end )
        {
            if( !m_task_dnd.actived )
            {
                m_task_dnd.actived = true;
                xapp_access_DND_enter();
            }
        }
        else 
        {
            if( m_task_dnd.actived )
            {
                m_task_dnd.actived = false;
                xapp_access_DND_exit();
            }
        }
    }
}

#endif // !XAPP_DND_ENABLE
